package com.wms.util;

import com.wms.entity.WmsStorageBatch;
import org.hibernate.Query;
import org.openbravo.dal.service.OBDal;
import org.openbravo.model.common.enterprise.Locator;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class GetStorageSizeUtil {
	/**
	 * 查询库存数量
	 * @param warehouseId 仓库ID
	 * @param productId 物料ID
	 * @param vendorId 供应商ID
	 * @return
	 */
	public synchronized static double getStroageSize(String warehouseId, String productId, String vendorId, String qualityGradeId) {
		return getStroageSize(warehouseId, productId, vendorId, qualityGradeId, null, null);
	}
	/**
	 * 跟条件查询库存量
	 * @param warehouseId 仓库ID
	 * @param productId 物料ID
	 * @param vendorId 供应商ID
	 * @param batchNo 指定物料批次
	 * @param useStatus 优先使用选用物料，NORMAL：正常、OTHER：选用
	 * @param qualityGradeId 品质级别
	 * @return
	 */
	public synchronized static double getStroageSize(String warehouseId, String productId, String vendorId, String qualityGradeId, String batchNo, String useStatus) {
		String sql = "select wms_get_Storage_Szie(?, ?, ?, ?, ?, ?) from dual";
		Query query = OBDal.getInstance().getSession().createSQLQuery(sql);
		query.setString(0, warehouseId);
		query.setString(1, productId);
		query.setString(2, vendorId == null ? "" : vendorId);
		query.setString(3, qualityGradeId == null ? "" : qualityGradeId);
		query.setString(4, batchNo == null ? "" : batchNo);
		query.setString(5, useStatus == null ? "" : useStatus);
		Object data = query.uniqueResult();
		if (data != null) {
			return new Double(data + "");
		}
		return 0;
	}
	/**
	 * 跟条件查询库存量
	 * @param warehouseId 仓库ID
	 * @param locatorId 货位ID
	 * @param productId 物料ID
	 * @param vendorId 供应商ID
	 * @param batchNo 指定物料批次
	 * @param useStatus 优先使用选用物料，NORMAL：正常、OTHER：选用
	 * @param qualityGradeId 品质级别
	 * @return
	 */
	public synchronized static double getLocatorStroageSize(String warehouseId, String locatorId, String productId, String vendorId,
															String qualityGradeId, String batchNo, String useStatus) {
		String sql = "select wms_get_Storage_Szie_New(?, ?, ?, ?, ?, ?, ?) from dual";
		Query query = OBDal.getInstance().getSession().createSQLQuery(sql);
		query.setString(0, warehouseId);
		query.setString(1, locatorId);
		query.setString(2, productId);
		query.setString(3, vendorId == null ? "" : vendorId);
		query.setString(4, qualityGradeId == null ? "" : qualityGradeId);
		query.setString(5, batchNo == null ? "" : batchNo);
		query.setString(6, useStatus == null || "".equals(useStatus) ? "NORMAL" : useStatus);
		Object data = query.uniqueResult();
		if (data != null) {
			return new Double(data + "");
		}
		return 0;
	}
	/**
	 * 查询仓位
	 * @param warehouseId 仓库ID
	 * @return
	 */
	public synchronized static List getLocator(String warehouseId) {
		String sql = "select l.m_locator_id from m_warehouse w,m_locator l "
				+ "where l.m_warehouse_id = w.m_warehouse_id and w.m_warehouse_id=? "
				+ "ORDER BY l.priorityno desc";
		Query query = OBDal.getInstance().getSession().createSQLQuery(sql);
		query.setString(0, warehouseId);
		List data = query.list();
		if (data != null) {
			return data;
		}
		return null;
	}
	/**
	 * 查询并返回库存信息
	 * @param warehouseId 仓库ID
	 * @param productId 物料ID
	 * @param vendorId 供应商ID
	 * @param qty 需求数量
	 * @return
	 */
	public synchronized static StorageInfo getStroageSizeDatail(String warehouseId, String productId, String vendorId, double qty) {
		return getStroageSizeDatail(warehouseId, null, productId, vendorId, qty, null, "NORMAL");
	}
	/**
	 * 查询并返回库存信息
	 * @param warehouseId 仓库ID
	 * @param locatorId 货位ID
	 * @param productId 物料ID
	 * @param vendorId 供应商ID
	 * @param qty 需求数量
	 * @param batchNo 指定物料批次
	 * @param useStatus 优先使用选用物料，NORMAL：正常、OTHER：选用
	 * @return
	 */
	public synchronized static StorageInfo getStroageSizeDatail(String warehouseId, String locatorId, String productId,
					String vendorId, double qty, String batchNo, String useStatus) {
		OBDal.getInstance().flush();
		
		String sql = "select wms_get_Locator_Storage_Mx(?, ?, ?, ?, ?, ?, ?) from dual";
		Query query = OBDal.getInstance().getSession().createSQLQuery(sql);
		query.setString(0, warehouseId);
		query.setString(1, locatorId);
		query.setString(2, productId);
		query.setString(3, vendorId);
		query.setDouble(4, qty);
		query.setString(5, batchNo == null ? "" : batchNo);
		query.setString(6, useStatus);
		
		String data = (String) query.uniqueResult();
		if (data != null) {
			BigDecimal size = new BigDecimal(data.split("#")[0]);
			StorageInfo sinfo = new StorageInfo();
			sinfo.setSize(size == null ? 0 : size.doubleValue());
			if (sinfo.getSize() > 0) {
				String[] datas = data.split("#")[1].split("-");
				StorageDetail detail = null;
				WmsStorageBatch storage = null;
				Locator locator = null;
				
				List<StorageDetail> list = new ArrayList<StorageDetail>();
				// 设置调拨单指定
				String[] info = null;
				for (String str : datas) {
					if (str.length() > 0) {
						info = str.split("\\|");
						storage = OBDal.getInstance().get(WmsStorageBatch.class, info[0]);
						locator = OBDal.getInstance().get(Locator.class, info[1]);
						
						detail = new StorageDetail();
						detail.setStorage(storage);
						detail.setLocator(locator);
						detail.setQty(new BigDecimal(info[2]));
						list.add(detail);
						//使用事物加锁，避免冲突
						OBDal.getInstance().save(storage);
					}
				}
				sinfo.setList(list);
			}
			return sinfo;
		}
		return null;
	}
	/**
	 * 返回库存的信息
	 */
	public static class StorageInfo {
		// 如果
		private double size = 0;
		private List<StorageDetail> list = null;

		/**
		 * 如果库存量大于需求数量，则此值为需求数量 如果库存量小于需求数量，则此值为库存量
		 */
		public double getSize() {
			return size;
		}
		public void setSize(double size) {
			this.size = size;
		}
		public List<StorageDetail> getList() {
			return list;
		}
		public void setList(List<StorageDetail> list) {
			this.list = list;
		}
	}

	/**
	 * 出库批次货位明细
	 */
	public static class StorageDetail {
		private WmsStorageBatch storage = null;
		private Locator locator = null;
		private BigDecimal qty = null;
		private String orgType = null;

		public WmsStorageBatch getStorage() {
			return storage;
		}
		public void setStorage(WmsStorageBatch storage) {
			this.storage = storage;
		}
		public Locator getLocator() {
			return locator;
		}
		public void setLocator(Locator locator) {
			this.locator = locator;
		}
		public BigDecimal getQty() {
			return qty;
		}
		public void setQty(BigDecimal qty) {
			this.qty = qty;
		}
		public String getOrgType() {
			return orgType;
		}
		public void setOrgType(String orgType) {
			this.orgType = orgType;
		}
	}
}
